package cn.com.nes.site.entity.bo.callcenter;

import cn.com.nes.mybatis.callcenter.entity.EquipmentCompany;
import cn.com.nes.site.entity.bo.DeviceSaveBo;
import cn.com.nes.site.entity.em.DeleteEnum;
import cn.com.nes.site.entity.vo.BaseVO;
import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import lombok.Data;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;

/**
 * 长效管护设备档案模型
 */
@Data
public class CallCenterEquipmentArchivesBo extends BaseVO {

    /**
     * 地点ID
     */
    private String ddId;

    // ----------用户信息---------
    /**
     * 联系人
     */
    private String contact;

    /**
     * 联系电话
     */
    private String contactPhone;

    /**
     * 身份证号
     */
    private String idCardNumber;

    /**
     * 行政区划代码
     */
    private String areaCode;

    /**
     * 厂商ID
     */
    private String manufactureId;

    /**
     * 项目地址
     */
    private String projectAddress;

    /**
     * 经度
     */
    private BigDecimal longitude;

    /**
     * 纬度
     */
    private BigDecimal latitude;

    /**
     * 长效管护公司ID
     */
    private Long callCenterCompanyId;

    // 设备信息
    /**
     * 设备列表
     */
    List<DeviceSaveBo> deviceSaveBos;

    /**
     * 数据来源
     */
    private String source;

    /**
     * 维修人员ID
     */
    private Long maintainanceUserId;

    /**
     * 转换房屋保存信息
     * @return
     */
    public HouseSaveBo toHouseSaveDto() {
        HouseSaveBo dto = new HouseSaveBo();
        dto.setDdId(this.getDdId());
        dto.setCompanyId(this.getCallCenterCompanyId());
        dto.setXzqhdm(this.getAreaCode());
        dto.setAddress(this.getProjectAddress());
        dto.setSource(this.getSource());
        dto.setGmtCreate(this.getOperateTimeStamp());
        dto.setGmtModified(this.getOperateTimeStamp());
        return dto;
    }

    /**
     * 转换客户保存信息
     * @return
     */
    public ClientSaveBo convertToClientSaveDto() {
        ClientSaveBo dto = new ClientSaveBo();
        dto.setCompanyId(this.getCallCenterCompanyId());
        dto.setName(this.getContact());
        dto.setPhone(this.getContactPhone());
        dto.setDelete(DeleteEnum._0.getKey());
        dto.setGmtCreate(this.getOperateTimeStamp());
        dto.setGmtModified(this.getOperateTimeStamp());
        dto.setDdId(this.getDdId());
        dto.setSource(this.getSource());
        return dto;
    }

    /**
     * 转换房屋客户关系保存数据
     * @param houseId
     * @param clientId
     * @return
     */
    public HouseClienteleSaveBo covertToHouseClienteleSaveDto(Long houseId, Long clientId) {
        HouseClienteleSaveBo dto = new HouseClienteleSaveBo();
        dto.setHouseId(houseId);
        dto.setClienteleId(clientId);
        dto.setGmtCreate(this.getOperateTimeStamp());
        dto.setGmtModified(this.getOperateTimeStamp());
        dto.setGmtCreate(this.getOperateTimeStamp());
        dto.setGmtModified(this.getOperateTimeStamp());
        return dto;
    }

    /**
     * 转换户主保存信息
     * @param clientInfoId 客户ID
     * @return
     */
    public MasterSaveBo convertToMasterSaveDto(Long clientInfoId) {
        MasterSaveBo dto = new MasterSaveBo();
        dto.setClienteleId(clientInfoId);
        dto.setCompanyId(this.getCallCenterCompanyId());
        dto.setName(this.getContact());
        dto.setPhone(this.getContactPhone());
        dto.setDelete(DeleteEnum._0.getKey());
        dto.setGmtCreate(this.getOperateTimeStamp());
        dto.setGmtModified(this.getOperateTimeStamp());
        dto.setSource(this.getSource());
        return dto;
    }

    /**
     * 转换房屋户主关系保存信息
     * @param houseInfoId  房屋ID
     * @param masterInfoId 户主ID
     * @return
     */
    public HouseMasterSaveBo convertToHouseMasterSaveDto(Long houseInfoId, Long masterInfoId) {
        HouseMasterSaveBo dto = new HouseMasterSaveBo();
        dto.setHouseId(houseInfoId);
        dto.setMasterId(masterInfoId);
        dto.setSource(this.getSource());
        dto.setGmtCreate(this.getOperateTimeStamp());
        dto.setGmtModified(this.getOperateTimeStamp());
        return dto;
    }

    /**
     * 转换设备保存信息
     * @param houseId
     * @param equipmentCompany
     * @return
     */
    public List<EquipmentSaveBo> toEquipmentSaveBoList(Long houseId, EquipmentCompany equipmentCompany) {
        List<EquipmentSaveBo> list = new ArrayList<>();
        if (CollUtil.isNotEmpty(this.getDeviceSaveBos())) {
            for (DeviceSaveBo deviceSaveBo : this.getDeviceSaveBos()) {
                EquipmentSaveBo bo = new EquipmentSaveBo();
                bo.setCompanyId(this.getCallCenterCompanyId()); // 通过登录用户的所属公司ID，查询长效管护对应的公司ID
                bo.setDdId(this.getDdId());
                bo.setHouseId(houseId);
                bo.setEquipmentNumber(deviceSaveBo.getDeviceSerialNo());
                if (StrUtil.isNotEmpty(deviceSaveBo.getDtuId())) {    // 前端传入dtuId，设备数据保存dtuId
                    bo.setTransmitNumber(deviceSaveBo.getDtuId());
                }
                bo.setSbLxid(deviceSaveBo.getDeviceModelId());
                bo.setInstallationSite("");
                bo.setSbId(deviceSaveBo.getSbId());
                bo.setImages(deviceSaveBo.getImages());
                bo.setLatitude(this.getLatitude());
                bo.setLongitude(this.getLongitude());
                bo.setContacts(this.getContact());
                bo.setContactstel(this.getContactPhone());
                bo.setAddress(this.getProjectAddress());
                if (ObjectUtil.isNotNull(equipmentCompany)) {
                    bo.setEquipmentCompanyId(equipmentCompany.getId());
                    bo.setEquipmentCompanyInfo(equipmentCompany.getName());
                }
                bo.setWarrantyExpiryDate(deviceSaveBo.getWarrantyDate());
                bo.setAzrq(this.getOperateTimeStamp());
                bo.setSource(this.getSource());
                bo.setPlatform(this.getOperatePlatform());
                bo.setGmtCreate(this.getOperateTimeStamp());
                bo.setGmtModified(this.getOperateTimeStamp());
                bo.setDelete(deviceSaveBo.getDeleted());
                bo.setIsPrivate(0);
                list.add(bo);
            }
        }
        return list;
    }


}
